<?php

namespace App\Services;

use App\Constants\CollectTypeConstant;
use App\Exceptions\BusinessException;
use App\Http\Resources\MemberCommentResource;
use App\Models\Article;
use App\Models\ArticleComment;
use App\Models\Collect;
use App\Models\Member;
use App\Models\MemberFollow;
use App\Models\Publisher;
use App\Models\Topic;
use App\Models\TopicComment;
use Illuminate\Support\Facades\DB;

class MemberService
{
    public function info(int $memberId, int $currentMemberId = 0): array
    {
        $member = Member::find($memberId);
        if (!$member) throw new BusinessException('用户不存在');

        $isSelf = $currentMemberId && $memberId == $currentMemberId;

        $member->load('publisher');
        $member->loadCount('topic');

        $followCount = MemberFollow::where('member_id', $memberId)->whereHas('follow_member')->count();
        $fansCount = MemberFollow::where('follow_id', $memberId)->whereHas('member')->count();

        if (!$isSelf) {
            $isFollow = MemberFollow::where('member_id', $currentMemberId)->where('follow_id', $memberId)->exists();
        }

        return [
            'id'           => $memberId,
            'nickname'     => $member->nickname,
            'avatar'       => $member->avatar,
            'sign'         => $member->sign,
            'favs_count'   => $member->favs_count,
            'relay_count'  => $member->relay_count,
            'follow_count' => $followCount,
            'topic_count'  => $member->topic_count,
            'fans_count'   => $fansCount,
            'is_vip'       => $member->is_vip,
            'is_self'      => $isSelf,
            'is_follow'    => $isFollow ?? false,
            'publisher'    => $member->publisher ? [
                'publisher_id' => $member->publisher->id,
            ] : null,
        ];
    }

    public function article(int $publisherId)
    {
        if (!Publisher::find($publisherId)) throw new BusinessException('发布人不存在');

        return Article::with(['publisher', 'comments', 'hot_news_tag'])
            ->where('type', 1)
            ->where('publish_id', $publisherId)
            ->orderByDesc('id')
            ->paginate();
    }

    public function topic(int $memberId, int $currentMemberId = 0)
    {
        $member = Member::find($memberId);
        if (!$member) throw new BusinessException('用户不存在');

        $isSelf = $currentMemberId && $memberId == $currentMemberId;

        return Topic::with(['member', 'topicTag:id,name', 'topicTheme:id,name'])
            ->where('member_id', $memberId)
            ->where(function ($q) use ($memberId, $isSelf) {
                return $isSelf ?
                    $q->whereRaw('IF(member_id = ?, status <= ?, status = ?)', [$memberId, 2, 1]) :
                    $q->where('status', 1);
            })
            ->orderByDesc('id')
            ->paginate();
    }

    public function collect(int $memberId)
    {
        $list = Collect::with('belong')->where([
            'member_id'   => $memberId,
            'belong_type' => CollectTypeConstant::TYPE_BY_DISEASE_DISCERN
        ])->orderByDesc('id')->paginate();

        return $list->items();
    }

    public function comment(int $memberId, int $currentMemberId = 0)
    {
        $member = Member::find($memberId);
        if (!$member) throw new BusinessException('用户不存在');

        $isSelf = $currentMemberId && $memberId == $currentMemberId;

        $articleComment = ArticleComment::with(['member', 'article', 'article.publisher'])
            ->select(['id', 'member_id', 'article_id', 'content', 'status', 'created_at'])
            ->addSelect(DB::raw('1 as type'))
            ->whereHas('article')
            ->where('member_id', $memberId)
            ->where(function ($q) use ($memberId, $isSelf) {
                return $isSelf ?
                    $q->whereRaw('IF(member_id = ?, status <= ?, status = ?)', [$memberId, 2, 1]) :
                    $q->where('status', 1);
            })->paginate(10);

        $articleComment = MemberCommentResource::collection($articleComment);

        $topicComments = TopicComment::with(['member', 'topic', 'topic.member'])
            ->select(['id', 'member_id', 'topic_id', 'content', 'status', 'created_at'])
            ->addSelect(DB::raw('2 as type'))
            ->whereHas('topic')
            ->where('member_id', $memberId)
            ->where(function ($q) use ($memberId, $isSelf) {
                return $isSelf ?
                    $q->whereRaw('IF(member_id = ?, status <= ?, status = ?)', [$memberId, 2, 1]) :
                    $q->where('status', 1);
            })->paginate(10);

        $topicComments = MemberCommentResource::collection($topicComments);

        return \collect($articleComment)->merge(\collect($topicComments))->sortByDesc('created_time')->values()->all();
    }
}
